﻿// Copyright (c) 2012 - 2020 Jared Zheng <jaredz at outlook dot com>
// Buddha Framework is licensed under Mulan PSL v2.
// You can use this software according to the terms and conditions of the Mulan PSL v2.
// You may obtain a copy of Mulan PSL v2 at:
//          http://license.coscl.org.cn/MulanPSL2
// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
// EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
// MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
// See the Mulan PSL v2 for more details.
//
// Create : 2012-12-01 version 0.1 Jared Zheng <jaredz at outlook dot com>
// Update : 2018-12-05 version 3.1.5 Jared Zheng <jaredz at outlook dot com>

#ifndef __ROUTINE_IMP_H__
#define __ROUTINE_IMP_H__

#pragma once

#include "IBalanceRoutine.h"

///////////////////////////////////////////////////////////////////////////////////////////////////////////
// CBalanceRoutine : 
// 1. 异步事件队列维护
// 2. 返回在线授权网关地址
// 3. 返回请求方外网地址
class CBalanceRoutine : public IBalanceRoutine
{
    DECLARE_RTTI_CREATE( CBalanceRoutine )
public:
    CBalanceRoutine(void);
    virtual ~CBalanceRoutine(void);
    // CEventHandler
    virtual UInt OnHandle(Int nEvent, uintptr_t utData, ULLong ullParam) OVERRIDE;
    virtual UInt OnHandle(Int nEvent, CEventBase&, ULLong) OVERRIDE;
    virtual UInt OnHandle(Int nEvent, CStream& Stream, ULLong ullParam) OVERRIDE;
    virtual UInt OnHandle(Int nEvent, UInt uCount) OVERRIDE;//
    // ICommonRoutine
    virtual bool Start(ICommonServer* pServer) OVERRIDE;
    virtual bool Pause(bool bPause = true) OVERRIDE;
    virtual bool Update(void) OVERRIDE;
    virtual void Stop(void) OVERRIDE;

    virtual UInt     GetThread(void) OVERRIDE;//
    virtual Socket_t GetSocket(UInt uType, UInt uId) OVERRIDE;//
    virtual bool     SetTimeout(UInt uTimeout = 0) OVERRIDE;

    virtual bool AddQueue(Int nEvent, uintptr_t utData = 0, ULLong ullParam = 0, QUEUE_TYPE eType = QUEUE_TYPE_NONE) OVERRIDE;
    virtual bool AddQueue(Int nEvent, CEventBase& EventRef, ULLong ullParam = 0, QUEUE_TYPE eType = QUEUE_TYPE_REFCOUNT) OVERRIDE;
    virtual bool AddQueue(Int nEvent, CStream& Stream, ULLong ullParam = 0) OVERRIDE;
private:
    bool  LoadExtConfig(bool bUpdate = false);
    bool  LoadExtBusiness(CKVNode& Root);
private:
    bool  GetAuthorAddr(ULLong ullParam);
    bool  GetExternalAddr(ULLong ullParam);

private:
    ICommonServer*        m_pServer;         ///< 框架例程处理对应的服务器
    time_t                m_tExtConfig;      ///< 扩展配置文件修改时间
    Int                   m_nTime;           ///< update()倍数次进行配置文件修改检测
    Int                   m_nUpdate;         ///< 
    UInt                  m_uThreads;        ///< [1, cores]
    UInt                  m_uTimeout;        ///< 异步事件队列工作线程超时检测的时间, 单位[S]
    CEventQueuePtr        m_EventQueuePtr;   ///< 事件队列, 每工作线程使用专属的redis连接
    CCommonRoutinePtr     m_BusinessPtr;     ///< 和产品业务相关
};

INLINE CBalanceRoutine::CBalanceRoutine(void)
: m_pServer(nullptr)
, m_tExtConfig(0)
, m_nTime(DATAD_UPDATE)
, m_nUpdate(0)
, m_uThreads(0)
, m_uTimeout(0)
{
    DEV_DEBUG(TF(" + %s new instance at %p"), GetRTTI().GetName(), this);
}

INLINE CBalanceRoutine::~CBalanceRoutine(void)
{
    DEV_DEBUG(TF(" - %s delete instance at %p"), GetRTTI().GetName(), this);
}

INLINE UInt CBalanceRoutine::OnHandle(Int, UInt)
{ 
    return RET_FAIL;
}

INLINE UInt CBalanceRoutine::GetThread(void)
{
    return m_uThreads;
}

INLINE Socket_t CBalanceRoutine::GetSocket(UInt, UInt)
{
    return 0;
}

#endif // __ROUTINE_IMP_H__
